package org.example.controllers;

import lombok.extern.slf4j.Slf4j;
import org.example.common.ApiResponse;
import org.example.common.NumberCheck;
import org.example.entitys.managers.Activity;
import org.example.service.imp.ActivityServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.Date;
import java.util.Objects;

/**
 * @Description
 * @Author 魏锴
 * @Date 2023/11/21
 */
@Controller
@Slf4j
@RequestMapping(value = "/activity")
@ResponseBody
public class ActivityController {
    @Autowired
    private ActivityServiceImp activityService;

    @RequestMapping
    public ApiResponse hello() {
        log.info("hello! 启动成功");
        return ApiResponse.ok();
    }

    @PostMapping(value = "/create")
    public ApiResponse createActivity(String name, String category, String creator, String title) {
        Activity activityVO = new Activity(name, category, creator, title);
        log.info("createActivity(" + activityVO + ")");
        return activityService.createActivity(activityVO) ? ApiResponse.ok(activityVO) : ApiResponse.failure();
    }

    @PostMapping(value = "/update")
    public ApiResponse updateActivity(Activity activityVO) {
        activityVO.setCreate_time(new Date());
        log.info("updateActivity(" + activityVO + ")");
        return activityService.updateActivity(activityVO) ? ApiResponse.ok(activityVO) : ApiResponse.failure();
    }

    @PostMapping("/publish")
    public ApiResponse startActivity(String id) {
        if (NumberCheck.isNotNumber(id)) return ApiResponse.paramIncorrect("id");
        log.info("startActivity(" + id + ")");
        return activityService.startActivity(Integer.parseInt(id)) ? ApiResponse.ok((Object) id) : ApiResponse.failure();
    }

    @PostMapping("/query")
    public ApiResponse queryActivity(String name, String status) {
        if (name == null && status == null) return ApiResponse.failure("请至少有一个参数");
        if (Objects.equals(status, "")) status = "-1";
        else if (NumberCheck.isNotNumber(status)) return ApiResponse.paramIncorrect("status");
        log.info("queryActivity( name: " + name + ", status: " + status + " )");
        Activity[] activities = activityService.queryActivityByNameAndStatus(name, Integer.parseInt(status));
        return ApiResponse.ok("status",status)
                .append("name",name)
                .append("所查询的活动", activities);
    }
}
